package com.bsg.upm.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class BeanUtils {

	public static Object clone(Object obj) {
		ByteArrayOutputStream byteOut = null;
		ByteArrayInputStream byteIn = null;
		ObjectOutputStream out = null;
		ObjectInputStream in = null;
		try {
			byteOut = new ByteArrayOutputStream();
			out = new ObjectOutputStream(byteOut);
			out.writeObject(obj);
			byteIn = new ByteArrayInputStream(byteOut.toByteArray());
			in = new ObjectInputStream(byteIn);
			return in.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (byteOut != null) {
				try {
					byteOut.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (byteIn != null) {
				try {
					byteIn.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
}
